package com.qf.contract.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.FileUploadUtils;
import com.qf.contract.entity.Emp;
import com.qf.contract.entity.FAnnouncementManagement;
import com.qf.contract.entity.FAnnouncementTypeOfContract;
import com.qf.contract.mapper.FAnnouncementManagementMapper;
import com.qf.contract.mapper.FAnnouncementTypeOfContractMapper;
import com.qf.contract.qo.FAnnouncementManagementQo;
import com.qf.contract.service.FAnnouncementManagementService;
import com.qf.contract.vo.FAnnouncementManagementListVo;
import com.qf.contract.vo.FAnnouncementManagementLookVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class FAnnouncementManagementServiceImpl implements FAnnouncementManagementService {
    /**
     * 引入文件上传对像
     */
    @Resource
    FileUploadUtils fileUploadUtils;
    @Resource
    private FAnnouncementManagementMapper fAnnouncementManagementMapper;
    @Resource
    private FAnnouncementTypeOfContractMapper fAnnouncementTypeOfContractMapper;


    /**
     * 新增公告
     */
    @Override
    public int addFAnnouncementManagementService(FAnnouncementManagementQo fAnnouncementManagementQo, MultipartFile file) {
        try {
            FAnnouncementManagement fAnnouncementManagement = new FAnnouncementManagement();
            /**
             * 通过标签名去标签表中查找标签id然后获取标签表id
             */
            MPJLambdaWrapper<FAnnouncementTypeOfContract> wrapper = new MPJLambdaWrapper<>();
            wrapper.eq("announcement_tag_name", fAnnouncementManagementQo.getAnnouncementLabel())
                    .last("limit 1");
            FAnnouncementTypeOfContract fAnnouncementTypeOfContract = fAnnouncementTypeOfContractMapper.selectOne(wrapper);
            /**
             * 讲他的主键id赋值给标题表
             */
            BeanUtils.copyProperties(fAnnouncementManagementQo, fAnnouncementManagement);
            fAnnouncementManagement.setAnnouncementTypeOfContract(fAnnouncementTypeOfContract.getAnnouncementTypeOfContractId());
            //添加创建时间
            fAnnouncementManagement.setAnnouncementStarDate(new Date());
            /**
             *文件上传
             */
            if (file != null) {
                //获取文件名
                String imgStar = fileUploadUtils.uploadImage(file);
                if (!ObjectUtils.isEmpty(imgStar)) {
                    //给对象赋值文件名
                    fAnnouncementManagement.setAnnouncementContract(imgStar);
                } else {
                    throw new ServiceException(ResultCode.IMG_NOT_EXIST);
                }
            }
            return fAnnouncementManagementMapper.insert(fAnnouncementManagement);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 删除公告
     *
     * @param id
     * @return
     */
    @Override
    public int detFAnnouncementManagementService(int id) {
        return fAnnouncementManagementMapper.deleteById(id);
    }

    /**
     * 编辑公告
     */
    @Override
    public int updateFAnnouncementManagementService(FAnnouncementManagementQo fAnnouncementManagementQo, MultipartFile file) {
        try {
            FAnnouncementManagement fAnnouncementManagement = new FAnnouncementManagement();
            /**
             * 通过标签名去标签表中查找标签id然后获取标签表id
             */
            if (fAnnouncementManagementQo.getAnnouncementLabel() != null) {
                MPJLambdaWrapper<FAnnouncementTypeOfContract> wrapper = new MPJLambdaWrapper<>();
                wrapper.eq("announcement_name", fAnnouncementManagementQo.getAnnouncementLabel());
                FAnnouncementTypeOfContract fAnnouncementTypeOfContract = fAnnouncementTypeOfContractMapper.selectOne(wrapper);
                /**
                 * 讲他的主键id赋值给标题表
                 */
                fAnnouncementManagement.setAnnouncementTypeOfContract(fAnnouncementTypeOfContract.getAnnouncementTypeOfContractId());
            }
            MPJLambdaWrapper<FAnnouncementManagement> wrapper = new MPJLambdaWrapper<>();
            /**
             *文件上传
             */
            if (file != null) {
                //获取文件名
                String imgStar = fileUploadUtils.uploadImage(file);
                if (!ObjectUtils.isEmpty(imgStar)) {
                    //给对象赋值文件名
                    fAnnouncementManagement.setAnnouncementContract(imgStar);
                } else {
                    throw new ServiceException(ResultCode.IMG_NOT_EXIST);
                }
            }
            /**
             * 在无参数传递时，将原数据库的数据填充进去
             */
            /**
             * 指定id修改
             */
            wrapper.eq("announcement_id", fAnnouncementManagement.getAnnouncementId());
            fAnnouncementManagement = fAnnouncementManagementMapper.selectById(fAnnouncementManagementQo.getAnnouncementId());
            BeanUtils.copyProperties(fAnnouncementManagementQo, fAnnouncementManagement);
            return fAnnouncementManagementMapper.update(fAnnouncementManagement, wrapper);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /*
    查看公告
     */
    @Override
    public FAnnouncementManagementLookVo lookFAnnouncementManagement(int id) {
        try {
            FAnnouncementManagement fAnnouncementManagement = fAnnouncementManagementMapper.selectById(id);
            FAnnouncementManagementLookVo fAnnouncementManagementLookVo = new FAnnouncementManagementLookVo();
            BeanUtils.copyProperties(fAnnouncementManagement, fAnnouncementManagementLookVo);
            return fAnnouncementManagementLookVo;
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }

    /**
     * 公告列表查看
     */
    @Override
    public IPage<FAnnouncementManagementListVo> fAnnouncementManagementList(int current, int size) {
        IPage<FAnnouncementManagementListVo> page;
        try {
            MPJLambdaWrapper<FAnnouncementManagement> wrapper = new MPJLambdaWrapper<FAnnouncementManagement>();
            wrapper.selectAll(FAnnouncementManagement.class)//查询user表全部字段
                    .selectAll(FAnnouncementTypeOfContract.class)
                    .select(Emp::getEmpName)
                    .leftJoin(Emp.class, Emp::getEmpId, FAnnouncementManagement::getAnnouncementPublisher)
                    .leftJoin(FAnnouncementTypeOfContract.class, FAnnouncementTypeOfContract::getAnnouncementTypeOfContractId, FAnnouncementManagement::getAnnouncementTypeOfContract);
            //        List<ContractRecordQueryOV> List = fContractMapper.selectJoinList(ContractRecordQueryOV.class, wrapper);
            wrapper.orderByDesc("announcement_priority");
            //TODO 分页连表查询
            page = fAnnouncementManagementMapper.selectJoinPage(new Page<>(current, size), FAnnouncementManagementListVo.class, wrapper);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
        return page;
    }

}
